import { routerRedux } from 'dva/router';
import api from '../api';
import { message } from 'antd'

export default {
    namespace: 'researchReport',
    state: {

    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                // 没有参数时，向后端请求数据，并且改变query
                if (pathname.indexOf('/customerService/researchReport') > -1) {
                    dispatch({ type: 'init', payload: query })
                }
            })
        }
    },
    effects: {
        * init({
            payload
        }, { call, put, select }) {
            // 初始化
            const state = yield select(state => state.researchReport)
            // 看本次的参数和上次的参数是否一致：
            // 一致的话，不请求，不一致的话，正常请求
            // 定义一个对象用于定义参数
            const newObj = {}
            for (var key in payload) {
                newObj[key] = state[key]
            }
            if (!Object.getOwnPropertyNames(payload).length || JSON.stringify(payload) != JSON.stringify(newObj)) {

                yield put({
                    type: 'setState',
                    payload,
                })
            }
        },

        * getMacroscopicalResearchList({
            payload
        }, { call, put, select }) {
            // 获取宏观研究列表数据
            const query = {}
            query.type=1073

            const res = yield call(api.getResearchList, query)
            yield put({
                type: 'setState', 
                payload: {
                    macroscopicalResearchList:res.data,
                }
            })
        },
        * getOperationalResearchList({
        payload
    }, { call, put, select }) {
        // 获取策略研究列表数据
        const query = {}
        query.type=1074
        const res = yield call(api.getResearchList, query)
        yield put({
            type: 'setState', 
            payload: {
                operationalResearchList:res.data,
            }
        })
    },
 
    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload
            }
        },
        initState(state) {
            return {}
        }
    }
}
